package service

import (
	"common/biz"
	"context"
	"core/dao"
	"core/entity"
	"core/repository"
	"core/xyError"
	"time"
	"user/pb"
)

type AccountService struct {
	accountDao *dao.AccountDao
	pb.UnimplementedUserServiceServer
}

func NewAccountService(manager *repository.DbManager) *AccountService {
	return &AccountService{
		accountDao: dao.NewAccountDao(manager),
	}
}

// Register 用户注册业务
func (account *AccountService) Register(ctx context.Context, param *pb.RegisterParams) (*pb.RegisterResponse, error) {
	uid := account.accountDao.IncrUid()
	ac := &entity.Account{
		WxAccount:  param.Account,
		CreateTime: time.Now(),
		Uid:        uid,
	}
	// 向 Mongo 插入数据
	if err := account.accountDao.Save(ctx, ac); err != nil {
		return nil, xyError.GrpcError(biz.SqlError)
	}
	return &pb.RegisterResponse{Uid: uid}, nil
}
